package com.springmvc.utils.method;

import java.util.List;

import com.springmvc.cache.CacheKey;
import com.springmvc.cache.MapCacheManager;
import com.springmvc.entity.MethodEnum;
import com.springmvc.utils.JsonUtils;
import com.springmvc.utils.result.ResultMsg;

import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

public class UserUtil {

	public static void getMajiang(Long roomid, Integer position) {

		MapCacheManager cache = MapCacheManager.getInstance();
		Long userid = (Long) cache.get(CacheKey.setRoomPositionUseridKey(roomid, position));
		if (userid == null) {
			System.err.println("用户不存在");
		}
		Channel channel = (Channel) cache.get(CacheKey.setChannelKey(userid));
		if (channel == null) {
			System.err.println("channel为空");
		}

		List<String> list = (List<String>) cache.get(CacheKey.setUsermajiangKey(roomid, position));
		channel.writeAndFlush(new TextWebSocketFrame(list.toString()));
	}

	public static void setMajiang(Long roomid, Integer position, List<String> list) {
		MapCacheManager cache = MapCacheManager.getInstance();
		Long userid = (Long) cache.get(CacheKey.setRoomPositionUseridKey(roomid, position));
		if (userid == null) {
			System.err.println("用户不存在");
			return;
		}
		Channel channel = (Channel) cache.get(CacheKey.setChannelKey(userid));
		if (channel == null) {
			System.err.println("channel为空");
			return;
		}
		cache.set(CacheKey.setUsermajiangKey(roomid, position), list);

//		String json = JsonUtils.obj2JsonString(ResultMsg.SUCCESS(MethodEnum.start, list));

//		channel.writeAndFlush(new TextWebSocketFrame(json));
	}

}
